<template>
    <el-dialog
        v-dialogDrag
        :title="dialogType == 2 ? '修改' : '新增'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        @close="handleClose"
        :center="true"
    >
        <!-- #弹窗表单 -->

        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
            <el-form-item label="分类名称" prop="name">
                <el-input v-model="dataForm.name" placeholder="分类名称"  clearable></el-input>
            </el-form-item>

            <el-form-item label="分类图标" prop="icon">
                <upload :img="dataForm.icon" @update="update" />
                <div class="sub-title">为了更好的展示效果，建议尺寸：300*300</div>
            </el-form-item>

            <el-form-item label="上级分类" prop="parentName" v-if="dialogType != 3">
                <el-popover ref="menuListPopover" placement="bottom-start" trigger="click" v-model="visible2">
                    <div style="overflow-y: auto; width: 200px; height: 200px">
                        <el-tree
                            :data="menuList"
                            :props="menuListTreeProps"
                            node-key="parentId"
                            ref="menuListTree"
                            @current-change="menuListTreeCurrentChangeHandle"
                            :default-expand-all="true"
                            :highlight-current="true"
                            :expand-on-click-node="false"
                        ></el-tree>
                    </div>
                </el-popover>

                <el-input
                    v-model="dataForm.parentName"
                    v-popover:menuListPopover
                    :readonly="true"
                    placeholder="点击选择上级分类"
                ></el-input>
            </el-form-item>

            <el-form-item label="排序号" prop="orderNo">
                <el-input-number v-model="dataForm.orderNo" controls-position="right" :min="1" label="排序号"> </el-input-number>
            </el-form-item>

            <el-form-item label="首页显示" prop="isHomePageShow">
                <el-checkbox v-model="isHomePageShow"></el-checkbox>
            </el-form-item>
        </el-form>

        <!-- #保存和提交按钮 -->
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import API from '@/api';
import upload from '../../../components/upLoad/index';
import cloneDeep from 'lodash/cloneDeep';

export default {
    components: {
        upload
    },
    data() {
        return {
            //el-dialog 弹窗
            dialogType: 1,
            visible: false,

            //表单
            dataForm: {
                name: '', //名称
                icon: '', //图标
                orderNo: '' //排序号
            },

            //表单中的上级分类
            visible2: false,
            menuList: [],
            menuListTreeProps: {
                label: 'name',
                children: 'childrens'
            },

            //表单中的 单选
            isHomePageShow: false,

            //表单验证
            dataRule: {
                name: [
                    //名称
                    {
                        required: true,
                        message: '分类名称不能为空',
                        trigger: 'blur'
                    }
                ],

                icon: [
                    //图标
                    {
                        required: true,
                        message: '分类图标不能为空',
                        trigger: 'change'
                    }
                ]
            }
        };
    },
    methods: {
        //- 弹窗关闭
        handleClose() {
            this.dataForm = {
                name: '',
                icon: '',
                orderNo: ''
            };
            this.$refs['dataForm'].resetFields();
        },

        //- 初始化
        init(item, type) {
            console.log(item);

            this.visible = true;
            this.getPageData();
            this.dialogType = type;

            if (item && type == 2) {
                //编辑

                this.dataForm = item;
                this.dataForm['classifyId'] = item.classifyId;

                //单选 处理  首页是否展示 (0:不展示;1:展示)
                this.isHomePageShow = item.homePageShow == 1 ? true : false;
            } else if (item && type == 1) {
                //列表新增

                this.dataForm['parentId'] = item.classifyId;
                this.dataForm['parentName'] = item.name;
                this.isHomePageShow = false;
            } else if (type == 3) {
                //页面顶部新增

                this.dataForm['parentId'] = '0';
                this.isHomePageShow = false;
            }
        },

        //获取页面数据 （ 上级分类 选择）
        getPageData() {

            let params ={};
            switch (this.$route.path) {//所属板块(CORPS: 农产品; APRODUCT: 农资产品)
                case '/goodsManger/goodsClassificationManger/ncp/index':
                    //农产品分类管理
                    params.plate='CORPS';

                    break;

                case '/goodsManger/goodsClassificationManger/nz/index':
                    //农资产品分类管理
                    params.plate='APRODUCT';
                    break;
            }
            API.goodsManger.getList(params).then((res) => {
                this.menuList = res.result;
            });
            
        },

        //表单：中的图片上传
        update(img) {
            this.dataForm.icon = img;
        },

        //- 表单 选择上级分类
        // 菜单树选中
        menuListTreeCurrentChangeHandle(data, node) {
            console.log(data);
            this.dataForm.parentId = data.classifyId;
            this.dataForm.parentName = data.name;
            this.visible2 = false;
        },

        // 菜单树设置当前选中节点
        menuListTreeSetCurrentNode() {
            this.$refs.menuListTree.setCurrentKey(this.dataForm.classifyId);
            this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name'];
        },

        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    var params = cloneDeep(this.dataForm);

                    //首页是否展示 (0:不展示;1:展示)
                    params.homePageShow = this.isHomePageShow == true ? '1' : '0';

                    //添加类型判断
                    switch (
                        this.$route.path //所属板块(CORPS: 农产品; APRODUCT: 农资产品)
                    ) {
                        case '/goodsManger/goodsClassificationManger/ncp/index':
                            //农产品分类管理
                            params.plate = 'CORPS';

                            break;

                        case '/goodsManger/goodsClassificationManger/nz/index':
                            //农资产品分类管理
                            params.plate = 'APRODUCT';
                            break;
                    }

                    var tick =
                        this.dialogType == 2 ? API.goodsManger.editCommodityClassify(params) : API.goodsManger.addCommodityClassify(params);

                    tick.then((data) => {
                        if (data.code == '0000') {
                            this.$message({
                                message: `${this.dialogType == 2 ? '修改成功' : '新增成功'}`,
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false;
                                    this.$emit('refreshDataList');
                                }
                            });
                        }
                    });
                }
            });
        }
    }
};
</script>